<?php
class Daotao_Model_Lophoc extends Zend_Db_Table{
	
	protected $_name = 'nam_daotao_lophoc';
	protected $_primary ='id';
	protected $_ids;
	
	// Cảnh
	public function itemInSelectbox($arrParam = null, $options = null){
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter, $config);
		if($options['task'] == 'get-lophoc'){
			$select = $db	->	select()
							->	from('nam_daotao_lophoc AS lh',array('id','name'))
							->	where('lh.status = ?', 1)
							->	order('lh.order ASC');
			
			if (isset($arrParam['id_khoahoc'])) {
				$select		->	where('lh.id_khoahoc = ?', $arrParam['id_khoahoc']);
			}			
			$result = $db	->	fetchPairs($select);
			$result[0] = '- Lựa chọn -';
			ksort($result);
		}
		return $result;
	}
	
	public function getItem($arrParam = null, $options = null){
		$db = Zend_Registry::get('connectDb');
		// $db = Zend_Db::factory($adapter, $config);
		if($options['task'] == 'admin-edit' || $options['task'] == 'admin-info'){
			$select = $db	-> select()
							-> from('nam_daotao_lophoc AS lh', array('*', "DATE_FORMAT(lh.ngaykhaigiang,'%d/%m/%Y') AS ngaykhaigiang", "DATE_FORMAT(lh.ngayketthuc,'%d/%m/%Y') AS ngayketthuc"))
							-> joinLeft('nam_daotao_khoahoc AS kh', 'kh.id = lh.id_khoahoc', array('kh.name AS khoahoc_name'))
							-> where('lh.id = ?', $arrParam['id']);
			$result = $db	-> fetchRow($select);
		}
		
		// Cảnh
		if($options['task'] == 'admin-edit-load'){
			$select = $db	-> select()
							-> from('nam_daotao_lophoc AS lh', array('soluongnguoidangky'))
							-> joinLeft('nam_daotao_khoahoc AS kh', 'kh.id = lh.id_khoahoc', array('soluongnguoitoida', 'price'))
							-> where('lh.id = ?', $arrParam['id_lophoc']);
			$result = $db	-> fetchRow($select);
		}
		
		// Cảnh
		if($options['task'] == 'admin-edit-load-hd') {
			$select = $db	-> select()
							-> from('nam_daotao_lophoc AS lh', array('name'))
							-> joinLeft('nam_daotao_khoahoc AS kh', 'kh.id = lh.id_khoahoc', array('soluongnguoitoida', 'price'))
							-> joinLeft('nam_hopdong AS hd', 'hd.id_lophoc = lh.id', array('COUNT(hd.id_lophoc) AS soluongnguoidangky'))
							-> where('lh.id = ?', $arrParam['id_lophoc']);
			$result = $db	-> fetchRow($select);
		}
		return $result;
	}
	
	public function countItem($arrParam = null, $options = null){
		$ssFilter  = $arrParam['ssFilter'];
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter, $config);
	
		if($options['task'] == 'admin-list'){
			$select = $db	-> 	select()
							-> 	from('nam_daotao_lophoc AS lh', array('COUNT(lh.id) AS totalItem'));
				
			if(!empty($ssFilter['keywords'])){
				$select->where("(lh.name LIKE '%" . $ssFilter['keywords'] . "%') OR (lh.id = '" . $ssFilter['keywords'] . "')");
			}
			
			if(!empty($ssFilter['id_khoahoc'])){
				$select->where('lh.id_khoahoc = ?', $ssFilter['id_khoahoc']);
			}
				
			$result = $db	-> fetchOne($select);
		}
	
		return $result;
	}
	
	public function listItem($arrParam = null, $options = null){
		$ssFilter  = $arrParam['ssFilter'];
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter, $config);
	
		if($options['task'] == 'admin-list'){
				
			$paginator = $arrParam['paginator'];
				
			$select = $db	->	select()
							->	from('nam_daotao_lophoc AS lh', array('*', "DATE_FORMAT(lh.ngaykhaigiang,'%d/%m/%Y') AS ngaykhaigiang", "DATE_FORMAT(lh.ngayketthuc,'%d/%m/%Y') AS ngayketthuc"))
							->	joinLeft('nam_daotao_khoahoc AS kh', 'lh.id_khoahoc = kh.id',array('kh.name AS khoahoc_name', 'kh.soluongnguoitoida AS khoahoc_soluongnguoitoida'));
	
			if(!empty($ssFilter['col']) && !empty($ssFilter['order'])){
				$select->order($ssFilter['col'] . ' ' . $ssFilter['order']);
			}
	
			if($paginator['itemCountPerPage'] > 0){
				$page = $paginator['currentPage'];
				$rowCount = $paginator['itemCountPerPage'];
				$select->limitPage($page, $rowCount);
			}
	
			if(!empty($ssFilter['keywords'])){
				$select->where("(lh.name LIKE '%" . $ssFilter['keywords'] . "%') OR (lh.id = '" . $ssFilter['keywords'] . "')");
			}
			
			if(!empty($ssFilter['id_khoahoc'])){
				$select->where('lh.id_khoahoc = ?', $ssFilter['id_khoahoc']);
			}
	
			$result = $db	-> fetchAll($select);
		}
	
		return $result;
	
	}
	
	public function saveItem($arrParam = null, $options = null){
	
		if($options['task'] == 'admin-add'){
			$row 				= $this->fetchNew();
	
			$info 					= new Zendvn_System_Info();
			$created_by 			= $info->getMemberInfo('id');
			
			if(!empty($arrParam['ngaykhaigiang'])){
				$date 				= new Zend_Date();
				$date->set($arrParam['ngaykhaigiang']);
				$ngaykhaigiang 		= $date->toString('YYYY-MM-dd');
			}
			
			if(!empty($arrParam['ngayketthuc'])){
				$date 				= new Zend_Date();
				$date->set($arrParam['ngayketthuc']);
				$ngayketthuc 			= $date->toString('YYYY-MM-dd');
			}
	
			$row->id_khoahoc		= stripslashes($arrParam['id_khoahoc']);
			$row->id_giaovien		= implode(',', $arrParam['id_giaovien']);
			$row->id_trogiang		= implode(',', $arrParam['id_trogiang']);
			$row->id_coach			= implode(',', $arrParam['id_coach']);
			$row->id_lichhoc		= implode(',', $arrParam['id_lichhoc']);
			$row->name 				= stripslashes($arrParam['name']);
			$row->content 			= stripslashes($arrParam['content']);
			$row->ngaykhaigiang		= $ngaykhaigiang;
			$row->ngayketthuc		= $ngayketthuc;
			$row->trangthai			= $arrParam['trangthai'];
			$row->order 			= $arrParam['order'];
			$row->status 			= $arrParam['status'];
			$row->created 			= @date("Y-m-d H:i:s");
			$row->created_by		= $created_by;
	
			$lastId = $row->save();
	
			$tblLogs = new Default_Model_Logs();
			$arrLogs = array(
					'id_user' 	=> $arrParam['adminInfo']['id'],
					'name'		=> 'Thêm lớp học mới - id ' . $lastId,
					'date'		=> @date('U')
			);
			$tblLogs->saveItem($arrLogs, array('task' => 'admin-add'));
		}
	
		if($options['task'] == 'admin-edit'){
	
			$where 				= ' id=' . $arrParam['id'];
			$row 				= $this->fetchRow($where);
	
			$info 					= new Zendvn_System_Info();
			$modified_by 			= $info->getMemberInfo('id');
				
			if(!empty($arrParam['ngaykhaigiang'])){
				$date 				= new Zend_Date();
				$date->set($arrParam['ngaykhaigiang']);
				$ngaykhaigiang 			= $date->toString('YYYY-MM-dd');
			}
			
			if(!empty($arrParam['ngayketthuc'])){
				$date 				= new Zend_Date();
				$date->set($arrParam['ngayketthuc']);
				$ngayketthuc 			= $date->toString('YYYY-MM-dd');
			}
	
			$row->id_khoahoc		= stripslashes($arrParam['id_khoahoc']);
			$row->id_giaovien		= implode(',', $arrParam['id_giaovien']);
			$row->id_trogiang		= implode(',', $arrParam['id_trogiang']);
			$row->id_coach			= implode(',', $arrParam['id_coach']);
			$row->id_lichhoc		= implode(',', $arrParam['id_lichhoc']);
			$row->name 				= stripslashes($arrParam['name']);
			$row->content 			= stripslashes($arrParam['content']);
			$row->ngaykhaigiang		= $ngaykhaigiang;
			$row->ngayketthuc		= $ngayketthuc;
			$row->trangthai			= $arrParam['trangthai'];
			$row->order 			= $arrParam['order'];
			$row->status 			= $arrParam['status'];
			$row->modified 			= @date("Y-m-d H:i:s");
			$row->modified_by		= $modified_by;
				
			$row->save();
				
			$lastId = $arrParam['id'];
				
			$tblLogs = new Default_Model_Logs();
			$arrLogs = array(
					'id_user' 	=> $arrParam['adminInfo']['id'],
					'name'		=> 'Sửa lớp học - id ' . $arrParam['id'],
					'date'		=> @date('U')
			);
			$tblLogs->saveItem($arrLogs, array('task' => 'admin-add'));
		}
	
		return $lastId;
	}
	
	public function deleteItem($arrParam = null, $options = null){
		if($options['task'] == 'admin-delete'){
			$where = ' id=' . $arrParam['id'];
			$result = $this->delete($where);
				
			$tblLogs = new Default_Model_Logs();
			$arrLogs = array(
					'id_user' 	=> $arrParam['adminInfo']['id'],
					'name'		=> 'Xóa lớp học - id ' . $arrParam['id'],
					'date'		=> @date('U')
			);
			$tblLogs->saveItem($arrLogs, array('task' => 'admin-add'));
		}
	
		if($options['task'] == 'admin-delete-muti'){
			$cid = explode(',', $arrParam['cid']);
			if(!empty($cid) && isset($arrParam['cid'])){
				$ids = implode(',', $cid);
				$where = 'id IN (' . $ids . ')';
				$this->delete($where);
	
				$tblLogs = new Default_Model_Logs();
				$arrLogs = array(
						'id_user' 	=> $arrParam['adminInfo']['id'],
						'name'		=> 'Xóa lớp học - id ' . $ids,
						'date'		=> @date('U')
				);
				$tblLogs->saveItem($arrLogs, array('task' => 'admin-add'));
			}
	
		}
	}
	
	public function changeStatus($arrParam = null, $options = null){
		$cid = $arrParam['cid'];
		if(count($cid) > 0){
			if($arrParam['type'] == 1){
				$status = 1;
			}else{
				$status = 0;
			}
	
			$id = implode(',', $cid);
			$data = array('status' => $status);
			$where = 'id IN (' . $id . ')';
			$this->update($data, $where);
				
			$tblLogs = new Default_Model_Logs();
			$arrLogs = array(
					'id_user' 	=> $arrParam['adminInfo']['id'],
					'name'		=> 'Thay đổi trạng thái lớp học - id ' . $id,
					'date'		=> @date('U')
			);
			$tblLogs->saveItem($arrLogs, array('task' => 'admin-add'));
		}
		if($arrParam['id'] > 0){
			if($arrParam['type'] == 1){
				$status = 1;
			}else{
				$status = 0;
			}
			$data = array('status' => $status);
			$where = 'id = ' . $arrParam['id'];
			$this->update($data, $where);
				
			$tblLogs = new Default_Model_Logs();
			$arrLogs = array(
					'id_user' 	=> $arrParam['adminInfo']['id'],
					'name'		=> 'Thay đổi trạng thái lớp học - id ' . $arrParam['id'],
					'date'		=> @date('U')
			);
			$tblLogs->saveItem($arrLogs, array('task' => 'admin-add'));
		}
	}
	
	public function sortItem($arrParam = null, $options = null){
		$cid = $arrParam['cid'];
		$id = implode(',', $cid);
		$order = $arrParam['order'];
		if($options['task'] == 'admin-sort'){
			if(count($cid) > 0){
				foreach ($cid as $key => $val){
					$data = array('order' => $order[$val]);
					$where = 'id = ' . $val;
					$this->update($data, $where);
				}
			}
		}
	
		$tblLogs = new Default_Model_Logs();
		$arrLogs = array(
				'id_user' 	=> $arrParam['adminInfo']['id'],
				'name'		=> 'Sắp xếp lớp học - id ' . $id,
				'date'		=> @date('U')
		);
		$tblLogs->saveItem($arrLogs, array('task' => 'admin-add'));
	}
}